set more off

foreach city in memphis nashville illinois {
	
	if "`city'" != "illinois" use "dataset_`city' wave 1", clear
	if "`city'" == "illinois" use "dataset_`city'", clear

	local X white female democrat homeowner baorabove age

	drop if mi(age)

	replace age = 	19	if age >=	18	& age <=	19
	replace age = 	20	if age >=	20	& age <=	20
	replace age = 	21	if age >=	21	& age <=	21
	replace age = 	23	if age >=	22	& age <=	24
	replace age = 	27	if age >=	25	& age <=	29
	replace age = 	32	if age >=	30	& age <=	34
	replace age = 	37	if age >=	35	& age <=	39
	replace age = 	42	if age >=	40	& age <=	44
	replace age = 	47	if age >=	45	& age <=	49
	replace age = 	52	if age >=	50	& age <=	54
	replace age = 	57	if age >=	55	& age <=	59
	replace age = 	60.5	if age >=	60	& age <=	61
	replace age = 	63	if age >=	62	& age <=	64
	replace age = 	65.5	if age >=	65	& age <=	66
	replace age = 	68	if age >=	67	& age <=	69
	replace age = 	72	if age >=	70	& age <=	74
	replace age = 	77	if age >=	75	& age <=	79
	replace age = 	82	if age >=	80	& age <=	84
	replace age = 	87.5	if age >=	85	& age <=	90

	gen n = age

	collapse (count) n, by(age)

	replace age = round(age)

	egen totaln = sum(n)

	su totaln
	replace n = n/totaln

	rename n n1

	save tmp, replace

	use "dataset_census", clear

	replace age = round(age)
	
	gen census = 1

	keep if city == "`city'"

	merge 1:1 age using tmp

	gen agecat2 = string(agemin) + "-" + string(agemax)
	
	replace agecat2 = "85+" if agecat2 == "85-90"
	
	labmask age, values(agecat2)

	levelsof age if age > 22, local(levels)

	erase tmp.dta
	
	if "`city'" == "memphis" {
		local mytext1 text(.125 66 "Sample")	
		local mytext2 text(.12 25 "Census", col(gray))
	}
	if "`city'" != "memphis" {
		local mytext1 
		local mytext2 
		replace n1 = 0 if agecat == "85 years and over"
	}

	local City = upper(substr("`city'", 1, 1)) + substr("`city'", 2, 50)
	
	keep if !mi(age)

	gen fl = 0

	replace n1 = 0 if mi(n1)

	#delimit;

	gr tw
		(rarea n2 fl age, col(gs12) mfcol(white) lwid(none) lcol(white))
		(line n1 age, col(black) lpat(solid) lwid(medthick))
		,
			legend(off)
			xlab(`levels', valuelabel angle(45) labsize(small))
			ylab(, angle(horiz))
			plotregion(style(none))
			xtitle("Age")
			ytitle("Density")
			`mytext1'
			`mytext2'
			name(g`city', replace)
			title("`City'")
			;

	#delimit cr

}

gr combine gmemphis gnashville gillinois, ycommon xcommon rows(3) ysize(3) xsize(2)

gr export "figure 1.eps", replace
shell epstopdf "figure 1.eps"
